import java.io.Serializable;
import java.util.Random;

/**
 * Represents the store. Knows the weight and costs of each item offered for
 * sale.
 */
public class Store  implements Serializable
{
	
	//TODO Change these to enums?
	public final static Item[] Item_List = {
		new Item("Oxen", 0, 4000),
		new Item("Food", 5, 100), 
		new Item("Clothing", 2, 1000), 
		new Item("Ammunition", 3, 200), 
		new Item("Wagon Wheel", 75, 1000), 
		new Item("Wagon Axle", 125, 1000), 
		new Item("Wagon Tongue", 100, 1000)};
	private int[] quantities;
	private int[] costs; //Costs are in cents, so 100 will be $1.00.
	
	/**
	 * Constructor for the Store class.
	 * @param storeNumber Used to determine costs and quantities of items in the store.
	 */
	public Store(int storeNumber)
	{
		Random r = new Random(storeNumber);
		quantities = new int[Item_List.length];
		costs = new int[Item_List.length];
		if(storeNumber == 0)
		{
			for(int i = 0; i < quantities.length; i++)
			{
				quantities[i] = 2000;
				costs[i] = Item_List[i].getCost();
			}
		}
		else
		{
			for(int i = 0; i < quantities.length; i++)
			{
				quantities[i] = (int) (r.nextDouble() * 1000 / Math.pow(1.25, storeNumber));
				costs[i] = (int) (Item_List[i].getCost() * Math.pow(1.25, storeNumber));
			}
		}
	}

	/**
	 * Accessor for the quantities field.
	 * @return Quantities array that holds the quantity of items held in the store. Indices correspond to indices in Item_List
	 */
	public int[] getQuantities()
	{
		return quantities;
	}

	/**
	 * Accessor for the costs field.
	 * @return Costs array that holds the costs of items held in the store. Indices correspond to indices in Item_List
	 */
	public int[] getCosts()
	{
		return costs;
	}
	
	public boolean removeItems(int[] quants)
	{
		boolean successful = true;
		if(quants == null)
		{
			return false;
		}
		
		for(int i = 0; i < quantities.length; i++)
		{
			if(quants[i] > quantities[i] || quants[i] < 0)
			{
				successful = false;
				
				//Removal unsuccessful, add items back to inventory
				for(int j = 0; j < i; j++)
					quantities[j] += quants[j];
				break;
			}
			else
				quantities[i] -= quants[i];
		}
		
		return successful;
	}
}
